// let USER = uni.getStorageSync('user') || {}
// if (typeof USER === 'string') {
// 	USER = JSON.parse(USER)
// }
let USER = uni.getStorageSync('user') || {}
import getTabbr from '@/config/tabbar.js';

import { defineStore } from 'pinia'
// 权限列表
let roleList = ['Purchase','Payment','Receive','ViewAgrement','TransferAccounts','Sale',
'Collection','SendOut','Promotion','Delivery','CategoryOfSeller','Agreement','ManageAgreement','Inventory','SellerHomePage',"ShopLevel",
'MyReport','Price','Category','Contract','Brand','Product','Expantion','BillOfEntry','SearchHongBao','BrandOwnerHomePage','BuyerReport','SellerReport','SellerAfterSales','BuyerAfterSales'
]
const getDefaultState = () => {
	let stateMenu = {};
	let userInfo =  uni.getStorageSync('user');
	if(userInfo){
		roleList.map(item=>{
			stateMenu['is'+item] = userInfo.menuCodes.includes(item);
		})
	}else{
		roleList.map(item=>{
			stateMenu['is'+item] = false;
		})
	}
	console.log(stateMenu);
	return stateMenu;
}
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useUserStore = defineStore('user', {
	state: () => ({
		// 用户token
		access_token: uni.getStorageSync('access_token') || '',
		// 用户信息
		user: uni.getStorageSync('user')||{},
		nickname:'',
		headImg:'',
		tabbarKey:'afujashgfkja',
		// 用户是否同意协议
		isAgree: uni.getStorageSync('agree') || false,
		checkStatus:uni.getStorageSync('checkStatus') || 0,
		tabbar:[],
		...getDefaultState()
	}),
	getters: {
		headerToken: state => {
			return {
				Authorization: 'Bearer ' + state.user.access_token,
			}
		}, 
		// 判断用户是否登录
		hasLogin: state => {
			return !!state.access_token;
		},
		//是否实名
		isAuthRealName:state=>{
			return state.user.realPeopleConfirmed;
		},
		//是否实名
		isApprovalIng:state=>{
			return state.user.code === 'PendingApproval';
		},
	},
	actions: {
		setTabbar(data) {
			this.tabbar = data
		},
		updateStatus(status){
			this.checkStatus = status;	
		},
		
		SET_USER_REALNAME:(status)=>{
			let  info = uni.getStorageSync('user');
			info.realPeopleConfirmed = status;
			uni.setStorageSync('user', info);
			this.user.realPeopleConfirmed = status;
		},
		login(info) {
			this.access_token = info.access_token
			uni.setStorageSync('access_token', info.access_token)
			delete info.access_token
			this.user = info;
			this.nickname = info.nickname;
			this.headImg = info.headImg;
			uni.setStorageSync('user', info);
		},
		// 用户修改信息
		updateInfo(data) {
			let info = USER
			const keys = Object.keys(data)
			for (let i = 0; i< keys.length; i++) {
				info[keys[i]] = data[keys[i]]
			}
			this.user = info;
			this.nickname = info.nickname;
			this.headImg = info.headImg;
			uni.setStorageSync('user', info);
			roleList.map(item=>{
				this['is'+item] = info.menuCodes.includes(item);
			})
		},
		agree() {
			this.isAgree = true
			uni.setStorageSync('agree', true)
		},
		logout() {
			this.access_token = '';
			this.user = {};
			uni.removeStorageSync('access_token');
			uni.removeStorageSync('user');
			uni.removeStorageSync('shop');
			getTabbr()
		}
	}
})
